var pageData = {
    "imageFile": null,
    "service": {
        "title": "",
        "type": "",
        "endTime": "",
        "address": "",
        "summary": "",
        "content": "",
        "image": "",
        "imageData": ""
    },
    "type": [
        {name:"课程",id:0},
        {name:"美容",id:1},
        {name:"美食",id:2},
        {name:"健康",id:3},
    ]
};



$(function () {

    // 初始化Vue实例
    var vue = new Vue({
        el: "#app",
        data: {
            pd: pageData
        },
        methods: {
            showSelect: function () {
                $("#fileInput").click();
            },
            upload: function () {
                // 上传图片
            },
            saveForm: function () {
                // 保存
                var formData = new FormData();
                formData.append("file",pageData.imageFile);
                formData.append("cTitle",pageData.service.title);
                formData.append("stopTime",pageData.service.endTime);
                formData.append("cSummary",pageData.service.summary);
                formData.append("cDetail",pageData.service.content);
                formData.append("cType",pageData.service.type);
                $.ajax({
                    url: '/service/addService',
                    type: 'post',
                    data: formData,
                    dataType: 'json',
                    xhrFields:{withCredentials:true},
                    processData: false,
                    contentType: false,
                    success: function (result) {
                        if(result){
                            layer.msg("服务添加成功");
                            window.location.href="/uc.html?apply=0";
                        }else{
                            layer.msg("请上传图片")
                        }

                    }
                })
            }
        }
    });

});

/**
 * 预览图片
 * @param source
 */
function previewImage(source) {
    var file = source.files[0];
    pageData.imageFile = file;
    if(window.FileReader){
        var fr = new FileReader();
        fr.onloadend = function (e) {
            pageData.service.imageData = e.target.result;
        };
        fr.readAsDataURL(file);
    }
}
